/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.hotfixformetroapp.main;

import com.seari.commonutils.redis.JedisUtilProxy;
import com.seari.hotfixformetroapp.bean.EmptyStringCell;
import com.seari.hotfixformetroapp.bean.ExcelConfig;
import static com.seari.hotfixformetroapp.main.HotfixGpsdataOfHistory.configPath;
import static com.seari.hotfixformetroapp.main.HotfixGpsdataOfHistory.properties;
import com.seari.hotfixformetroapp.utils.ImportExcelUtil;
import com.seari.hotfixformetroapp.utils.MiscTools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Rainbow
 */
//@Component
//@Order(value = 2)
public class HotfixJiudaoData implements ApplicationRunner
{

    public static Properties properties;
    public static String configPath = "config.cfg"; //配置文件路径
    private final ExcelConfig dataConfig = new ExcelConfig();
    private Map<String,String> resultMap = new HashMap<String,String>();
    private Jedis jedis;

    @Override
    public void run(ApplicationArguments args) throws Exception
    {
        System.out.println("jiudao data");
        JedisUtilProxy.initJedisPoolUtil("127.0.0.1", 6379);
        jedis = JedisUtilProxy.getJedis();
        init();
        dataConfig.setSheetNum(Integer.parseInt(properties.getProperty("sheetNum", "1")) - 1); //程序中以0为起始，需减1
        dataConfig.setDataStartLineNum(Integer.parseInt(properties.getProperty("dataStartLineNum", "2")) - 1);
        dataConfig.setDataEndLineNum(Integer.parseInt(properties.getProperty("dataEndLineNum", "2")) - 1);
        dataConfig.setInputFileName(properties.getProperty("inputFileName"));
        dataConfig.setOutputFileName(properties.getProperty("outputFileName"));
        InputStream inputStream = new FileInputStream(new File(dataConfig.getInputFileName()));
        Workbook workbook = ImportExcelUtil.getWorkbook(inputStream, dataConfig.getInputFileName());
        process(workbook);
    }

    private void process(Workbook workbook) throws IOException
    {
        FileOutputStream fileOutputStream = null;
        Workbook outwb = new HSSFWorkbook();
        Sheet outsheet = outwb.createSheet();
        int rowCount = 0;
        try
        {
            Sheet sheet = workbook.getSheetAt(dataConfig.getSheetNum());
            for (int i = dataConfig.getDataStartLineNum(); i <= dataConfig.getDataEndLineNum(); i++)//仅读取起始至结尾的表格数据
            {
                Row row = sheet.getRow(i);
                for (int j = 0; j < 2; j++)
                {
                    //获取单元格
                    Cell cell = row.getCell(j);
                    if (cell != null)
                    {
                        //强制单元格为String类型数据
                        cell.setCellType(CellType.STRING);
                        String key = cell.getStringCellValue();
                        if(StringUtils.isBlank(key))continue;
                        String value = jedis.hget("line17_DI", key);
                        Row outrow = outsheet.createRow(rowCount);
                        outrow.createCell(0).setCellValue(key);
                        outrow.createCell(1).setCellValue(value);
                        rowCount++;
                    }
                }
            }
            fileOutputStream = new FileOutputStream(new File(dataConfig.getOutputFileName()));
            outwb.write(fileOutputStream);
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            if (fileOutputStream != null)
            {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
            outwb.close();
            JedisUtilProxy.close(jedis);
        }
    }

    private void init()
    {
        try
        {
            properties = MiscTools.loadConfig(configPath);
        } catch (Exception e)
        {
        }
    }

}
